﻿using System;

namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
{
    /// <summary>
    /// 字符串和枚举的转换
    /// </summary>
    /// <typeparam name="TPropertyType"></typeparam>
    public class YddEnumConverter<TPropertyType> : ValueConverter<TPropertyType, string> where TPropertyType : struct, Enum
    {
        public YddEnumConverter()
            : base(
                d => SerializeObject(d),
                s => DeserializeObject(s))
        {

        }

        private static string SerializeObject(TPropertyType d)
        {
            return d.GetEnumMemberValue();
        }

        private static TPropertyType DeserializeObject(string s)
        {
            return StringExtension.ConversionToEnums<TPropertyType>(s);
        }
    }
}
